<?php

require_once('admincontroller.php');

class Posts extends AdminController
{
	function Posts()
	{
		parent::AdminController();
		$this->load->model('Post');
		$this->main_navigation = 'site';
		$this->sub_navigation = 'posts';
	}
	
	// List de posts
	function index()
	{
		$data['posts'] = $this->Post->all();	
		$this->display('post_list', $data);
	}
	
	// Page de création d'un nouveau post
	function new_post()
	{
		$data['post'] = new Post();
		$data['tags'] = array();
		$data['all_tags'] = $this->Post->all_tags();
		$this->display('post_new', $data);
	}
	
	// Page d'édition d'un post
	function edit($id)
	{
		$data['post'] = $this->Post->get_by_id($id);
		$data['tags'] = $this->Post->tags($id);
		$data['all_tags'] = $this->Post->all_tags();
		$this->display('post_edit', $data);
	}
	
	// Création d'un nouveau post
	function create()
	{
		$post = new Post();
		$post->creator_id = $post->updater_id = $this->session->userdata('user_id');
		$post->created_at = $post->updated_at = date_db_format();
		
		$errors = $this->Post->update($post, $_POST);

		if (count($errors) == 0 && $this->Post->save($post)) {
			$tag_names = $this->Post->update_tags($post, $_POST['tag_names']);
			$data['success_notice'] = 'Article créé !<br />Marqué comme : '.implode(', ', $tag_names);
			$data['posts'] = $this->Post->all();
			$this->display('post_list', $data);			
		}
		else {
			$data['error_notice'] = $errors;
			$data['post'] = $post;
			$data['tags'] = array();
			$data['all_tags'] = $this->Post->all_tags();
			$this->display('post_new', $data);
		}	
	}
	
	// Mise à jour du post
	function update($id)
	{
		$post = $this->Post->plain_get($id);
		$post->updater_id = $this->session->userdata('user_id');
		$post->updated_at = date_db_format();
		
		$errors = $this->Post->update($post, $_POST);
		
		if (count($errors) == 0 && $this->Post->save($post)) {
			$tag_names = $this->Post->update_tags($post, $_POST['tag_names']);
			$data['success_notice'] = 'Article mis à jour !<br />Marqué comme : '.implode(', ', $tag_names);
			$data['posts'] = $this->Post->all();
			$this->display('post_list', $data);
		}
		else {
			$data['error_notice'] = $errors;
			$data['post'] = $post;
			$data['tags'] = $this->Post->tags($id);
			$data['all_tags'] = $this->Post->all_tags();
			$this->display('post_edit', $data);
		}
	}
	
	// Suppression d'un post
	function delete($id)
	{
		if (!$this->Post->delete($id)) {
			$this->output->set_status_header(400);
		}
	}
	
	function restore($id)
	{
		if ($this->Post->restore($id)) {
			$data['success_notice'] = "Suppression de l'article annulée";
		}
		else {
			$data['error_notice'] = "Impossible de restaurer l'article";
		}
		$data['posts'] = $this->Post->all();
		$this->display('post_list', $data);
	}
	
	// Détails d'un post
	function details($id)
	{
		$data['post'] = $this->Post->get_by_id($id);
		$data['tags'] = $this->Post->tags($id);
		
		$this->load->view('admin/posts/post_details', $data);
	}
	
	function tags()
	{
		$data['tags'] = $this->Post->all_tags();
		$this->display('post_tags', $data);
	}
}
